<template>
  <div class="login">
    <div id="login_Small">
      <div class="login_img">
        <img src="../assets/logo.png" />
      </div>
      <div class="login_second">
        <el-form
          :model="ruleForm"
          :rules="rules"
          ref="ruleForm"
          label-width="70px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username">
            <el-input v-model="ruleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="ruleForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >立即登录</el-button
            >
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      ruleForm: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    // async submitForm() {
    //   let res = await login({
    //     username: this.ruleForm.name,
    //     password: this.ruleForm.password,
    //   });
    //   console.log(res);
    //   if (res.meta.status == 200) {
    //     alert(res.meta.msg);
    //     localStorage.setItem("token", res.data.token);
    //     this.$router.push("/home");
    //   } else {
    //     alert(res.meta.msg);
    //   }
    // },

    // async submitForm(formName) {
    //   let res = await login({
    //     username: this.ruleForm.name,
    //     password: this.ruleForm.password,
    //   });
    //   this.$refs[formName].validate((valid) => {
    //     if (valid) {
    //       if (res.meta.status == 200) {
    //         alert(res.meta.msg);
    //         localStorage.setItem("token", res.data.token);
    //         this.$router.push("/home");
    //       }
    //     } else {
    //       return false;
    //     }
    //   });
    // },

    async submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$store
            .dispatch("users/loginFgn", 
             this.ruleForm
            )
            .then(
              () => {
                this.$router.push("/home");
              },
              () => {
                this.resetForm("ruleForm");
              }
            );
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: 100%;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;
  #login_Small {
    width: 50vh;
    height: 36vh;
    background-color: #efefef;
    box-shadow: 0px 0px 3vh white;
    display: flex;
    justify-content: center;
    position: relative;
    .login_img {
      width: 23vh;
      height: 23vh;
      background-color: white;
      position: absolute;
      top: -100px;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 50%;
      img {
        width: 20vh;
        height: 20vh;
        border-radius: 50%;
        background-color: #efefef;
      }
    }
    .login_second {
      margin-top: 11vh;
    }
  }
}
</style>